<template>
  <div id="DataMaintenance-ClientType">
    <div class="flex-all pt10 pb10 pl12 pr12 bg-white height50">
      <div class="ct-head-left flex-al">
        <span class="left-line-orange mr10"></span>
        <span class="color-333 fs14">{{$t('enrollStudentSet.enrollStuSettings.ClientType.50zg4h5dhlo0')}}</span>
      </div>
      <div class="cr-head-right">
        <el-button type="primary" size="mini" @click="handAdd">{{$t('enrollStudentSet.enrollStuSettings.ClientType.50zg4h5diyk0')}}</el-button>
      </div>
    </div>
    <div class="pAll12 bt_EBEEF5">
      <div class="flex-al Inquire-box mb12">
        <div class="ipt-box" style="width:250px;">
          <el-input
            v-model="filter.keyword"
            clearable
            :placeholder="$t('enrollStudentSet.enrollStuSettings.ClientType.50zg4h5djd00')"
            @clear="search"
            @keyup.enter.native="search"
          ></el-input>
        </div>
        <el-button type="primary" size="mini" class="search-btn fs14" @click="search">{{$t('enrollStudentSet.enrollStuSettings.ClientType.50zg4h5dljc0')}}</el-button>
      </div>
      <div class="tableDiv" ref="auto_height_table">
        <settingTableCont :tableData="tableList">
          <!-- 按钮操作组********start -->
          <el-table-column ref="fixedColumn" :label="$t('enrollStudentSet.enrollStuSettings.ClientType.50zg4h5dm9c0')" align="center" width="120px" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click.native.prevent="handEdit(scope.row)">{{$t('enrollStudentSet.enrollStuSettings.ClientType.50zg4h5dmm00')}}</el-button>
              <el-button type="text" size="small" @click.native.prevent="handDelete(scope.row)">{{$t('enrollStudentSet.enrollStuSettings.ClientType.50zg4h5dmx40')}}</el-button>
            </template>
          </el-table-column>
          <!-- 按钮操作组********end -->
        </settingTableCont>
        <!-- 分页 -->
        <el-pagination
          style="margin: 10px;"
          :current-page.sync="paging.curPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size.sync="paging.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="paging.pageTotal"
          @current-change="curPageChange"
          @size-change="pageSizeChange"
        ></el-pagination>
      </div>
    </div>
    <!-- 侧边弹窗******新增或者编辑名单类型 -->
    <el-drawer
      :title="isEdit?$t('enrollStudentSet.enrollStuSettings.ClientType.add1'):$t('enrollStudentSet.enrollStuSettings.ClientType.add2')"
      :visible.sync="cancelVisible"
      direction="rtl"
      size="600px"
      :modal="false"
      custom-class="pop-slider--box"
      :before-close="handleClose"
    >
      <div class="slider-btn">
        <el-button v-if="!isEdit" type="primary" size="mini" @click="onSubmit">{{$t('enrollStudentSet.enrollStuSettings.ClientType.50zg4h5dn7w0')}}</el-button>
        <el-button v-else type="primary" size="mini" @click="onEditSubmit">{{$t('enrollStudentSet.enrollStuSettings.ClientType.50zg4h5dn7w0')}}</el-button>
      </div>
      <div class="slider-content pAll20">
        <el-form label-width="130px" :model="ruleForm" :rules="rules" ref="ruleForm">
          <el-form-item :label="$t('enrollStudentSet.enrollStuSettings.ClientType.50zg4h5dnj40')" prop="clienttype_name">
            <el-input
              clearable
              :placeholder="$t('enrollStudentSet.enrollStuSettings.ClientType.50zg4h5dntw0')"
              v-model="ruleForm.clienttype_name"
              style="width:320px;"
            ></el-input>
          </el-form-item>
          <el-form-item :label="$t('enrollStudentSet.enrollStuSettings.ClientType.50zg4h5do400')" prop="clienttype_remk">
            <el-input
              type="textarea"
              clearable
              v-model="ruleForm.clienttype_remk"
              autosize
              maxlength="200"
              show-word-limit
              :placeholder="$t('enrollStudentSet.enrollStuSettings.ClientType.50zg4h5dois0')"
              style="width:320px;"
            ></el-input>
          </el-form-item>
          <p class="color-red">* {{$t('enrollStudentSet.enrollStuSettings.ClientType.50zg4h5doyk0')}}</p>
        </el-form>
      </div>
    </el-drawer>
    <!-- 删除名单类型 -->
    <popTips
      :dialogVisable="delItem"
      :popTipsCont="popTips_del"
      @handConfirm="delClientTypeAction"
      @handCancel="delItem=false"
      @handleCloseTip="delItem=false"
    ></popTips>
  </div>
</template>

<script>
import settingTableCont from "@/components/tables/settingTableCont.vue";
import popTips from "@/components/common/popTips/popTips.vue";
import setHttp from "../../Set";

export default {
  name: "setting",
  components: {
    settingTableCont,
    popTips
  },
  data() {
    return {
      delItem: false, //删除名单类型
      cancelVisible: false, //新增或者编辑名单类型
      isEdit: false, //是否编辑名单类型
      clienttypeId: "",
      //删除名单类型****弹窗
      popTips_del: {
        btn_text1: this.$t('enrollStudentSet.enrollStuSettings.ClientType.50zg4h5dpbk0'),
        btn_text2: this.$t('enrollStudentSet.enrollStuSettings.ClientType.50zg4h5dpw00'),
        title: this.$t('enrollStudentSet.enrollStuSettings.ClientType.50zg4h5dq900'),
        context: this.$t('enrollStudentSet.enrollStuSettings.ClientType.50zg4h5dqn40')
      },

      //筛选
      filter: {
        keyword: ""
      },
      // 表格数据
      tableList: {
        // 表格的列
        columns: [],
        // 表格数据
        list: [],
        options: {
          errortip: "", //空页面提示内容
          mutiSelect: false, //是否显示多选
          issetting: false, //是否可以编辑列
          tableHeight: 0
        }
      },
      // 分页
      paging: {
        curPage: 1,
        pageSize: 10,
        pageTotal: 0
      },
      // 表单
      ruleForm: {
        clienttype_name: "",
        clienttype_remk: ""
      },
      // 表单验证
      rules: {
        clienttype_name: [
          {
            required: true,
            message: this.$t('enrollStudentSet.enrollStuSettings.ClientType.50zg4h5dqz80'),
            trigger: "blur"
          },
          { min: 2, max: 16, message: '长度在2到16个字符', trigger: "blur" },
        ],
        clienttype_remk: [
          {
            required: false,
            message: "",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.getClientTypeList();
  },
  methods: {
    // 招生相关设置->获取名单类型列表 -- wgh
    getClientTypeList() {
      setHttp
        .getClientTypeList({
          is_count: "1", //是否需要总数，可以不传
          p: this.paging.curPage,
          num: this.paging.pageSize,
          //筛选条件
          keyword: this.filter.keyword
        })
        .then(res => {
          this.paging.pageTotal = parseInt(res.data.result.allnum);
          this.tableList.columns = res.data.result.field;
          console.log(res);
          if (res.data.error == 0) {
            this.tableList.list = res.data.result.list;
          } else {
            this.tableList.list = [];
            this.tableList.options.errortip = res.data.errortip;
          }
        });
    },

    //检索
    search() {
      this.paging.curPage = 1;
      this.getClientTypeList();
    },
    //新增
    handAdd() {
      this.isEdit = false;
      this.cancelVisible = !this.cancelVisible;
      this.resetForm("ruleForm");
    },
    //编辑
    handEdit(v) {
      this.clienttypeId = v.clienttype_id;
      this.resetForm("ruleForm");
      this.ruleForm = {
        clienttype_name: v.clienttype_name,
        clienttype_remk: v.clienttype_remk
      };
      this.isEdit = true;
      this.cancelVisible = !this.cancelVisible;
      console.log(v);
    },
    //删除
    handDelete(row) {
      this.clienttypeId = row.clienttype_id;
      this.delItem = !this.delItem;
    },
    //招生相关设置->删除名单类型 -- wgh
    delClientTypeAction() {
      setHttp
        .delClientTypeAction({
          clienttype_id: this.clienttypeId
        })
        .then(res => {
          console.log(res);
          if (res.data.error == 0) {
            this.$message({ message: res.data.errortip, type: "success" });
            this.delItem = false;
            this.getClientTypeList();
          } else {
            this.$message.error(res.data.errortip);
          }
        });
    },
    //侧边弹窗 - 新增 - 提交
    onSubmit() {
      let canSubmit = this.$Tool.isNull("ruleForm", this);
      if (canSubmit) {
        setHttp
          .addClientTypeAction({
            clienttype_name: this.ruleForm.clienttype_name,
            clienttype_remk: this.ruleForm.clienttype_remk
          })
          .then(res => {
            console.log(res);

            if (res.data.error == 0) {
              this.$message({ message: res.data.errortip, type: "success" });
              this.cancelVisible = false;
              this.getClientTypeList();
            } else {
              this.$message.error(res.data.errortip);
            }
          });
      }
    },
    //侧边弹窗 - 编辑 - 提交
    onEditSubmit() {
      let canSubmit = this.$Tool.isNull("ruleForm", this);
      if (canSubmit) {
        setHttp
          .updateClientTypeAction({
            clienttype_id: this.clienttypeId,
            clienttype_name: this.ruleForm.clienttype_name,
            clienttype_remk: this.ruleForm.clienttype_remk
          })
          .then(res => {
            console.log(res);
            if (res.data.error == 0) {
              this.$message({ message: res.data.errortip, type: "success" });
              this.cancelVisible = false;
              this.getClientTypeList();
            } else {
              this.$message.error(res.data.errortip);
            }
          });
      }
    },
    // 复选框
    handleSelectionChange(v) {
      console.log(v);
    },
    // 页码变化
    curPageChange(v) {
      this.paging.curPage = v;
      this.getClientTypeList();
    },
    // pageSize改变
    pageSizeChange() {
      this.paging.curPage = 1;
      this.getClientTypeList();
    },
    // 重置表单
    resetForm(form) {
      // 数据清空
      this.ruleForm = {
        clienttype_name: "",
        clienttype_remk: ""
      };
      if (this.$refs[form]) {
        this.$refs[form].resetFields();
      }
    },
    //侧边弹窗-关闭
    handleClose(done) {
      done();
    }
  },
  mounted() {
    this.$nextTick(() => {
      if (this.$refs["auto_height_table"]) {
        this.tableList.options.tableHeight = this.$Tool.getTableHeight(
          this.$refs.auto_height_table.getBoundingClientRect().top + 55
        );
      }
    });
  }
};
</script>

<style lang="less">
// #DataMaintenance-ClientType {
// }
</style>
